<?php

/**
 * user actions.
 *
 * @package    blogallery
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class userActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex($request)
  {
    $this->forward('user', 'show');
  }
  
  public function executeList($request)
  {
  	$c = UserPeer::getPublicUserCriteria();
  	
    $pager = new sfPropelPager('User', sfConfig::get('app_max_user'));
    $pager->setCriteria($c);
    $pager->setPage($request->getParameter('page') ? $request->getParameter('page') : 1);
    $pager->init();
    
    $this->pager = $pager;
    
  	myTools::prependTitle('Users');
  }
  
  public function executeShow($request)
  {
  	if ($request->getParameter('username'))
  	{
      $user = UserPeer::retrievePublicByUsername($request->getParameter('username'));
  	}
  	else
  	{
  	  $user = UserPeer::retrieveByPk($this->getUser()->getId());
  	}
  	
    $this->forward404Unless($user instanceof User);
        
    $this->user = $user;
    $this->posts = $user->getRecent();    
    
  	$this->getResponse()->addJavascript('livepipe.js', 'last');
  	$this->getResponse()->addJavascript('window.js', 'last'); 
  }
  
  public function executeCreate($request)
  {
  	$form = new UserForm();
  	 
  	$this->getResponse()->addJavascript('livepipe.js', 'last');
  	$this->getResponse()->addJavascript('tabs.js', 'last'); 

  	$this->form = $form;
  	$this->setTemplate('edit');  	
  }
    
  public function executeEdit($request)
  {
  	if ($this->getUser()->hasCredential('admin'))
  	{
  	  $id = $request->getParameter('id') ? $request->getParameter('id') : $this->getUser()->getId();
  	  $user = UserPeer::retrieveByPK($id);
  	}
  	else
  	{
  	  $user = UserPeer::retrieveByPK($this->getUser()->getId());
  	}
  	
  	$this->forward404Unless($user instanceof User);  
  		
  	$form = new UserForm($user); 
  	
  	$this->getResponse()->addJavascript('livepipe.js', 'last');
  	$this->getResponse()->addJavascript('tabs.js', 'last'); 
  	
  	if ($request->isMethod('post'))
  	{
  	  $form->bind($request->getParameter('user'), $request->getFiles('user'));
  	  
  	  if ($form->isValid())
  	  {
  	  	$form->save();
  	  	return $this->redirect('@default?module=user&action=index');
  	  }
  	} 
  	
  	$this->form = $form;  	
  }
    
  public function executeUpdate($request)
  {
  	if ($request->isMethod('post'))
  	{
  	  if ($request->getParameter('user[id]'))
  	  {
  	    $user = UserPeer::retrieveByPK($request->getParameter('user[id]'));
  	  	$this->forward404Unless($user instanceof User);
  	  }
  	  else
  	  {
  	  	$user = new User();
  	  }
  	  
  	  if (!$this->getUser()->hasCredential('admin'))
  	  {
  	    $this->forward404If($user->isNew());
  	    $this->forward404Unless($user->getId() === $this->getUser()->getId());  	  		
  	  }
  	  
  	  $form = new UserForm($user);  	  

  	  $form->bind($request->getParameter('user'), $request->getFiles('user'));
  	  
  	  if ($form->isValid())
  	  {
  	  	$file = $form->getValue('file');
  	  	
  	  	if ($file)
  	  	{
  	  	  $salt = myTools::makeRandomString();
          $filename = sha1($salt.$file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
          $file->save(sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.$filename);
  	  	}
  	  	        
   	  	$form->save();
        return $this->redirect('@default?module=user&action=index');
  	  }
  	  else
  	  {
  	  	$this->form = $form; 
  	  	 	  	
  	    $this->getResponse()->addJavascript('livepipe.js', 'last');
  	    $this->getResponse()->addJavascript('tabs.js', 'last');
  	     
  	  	$this->setTemplate('edit');
  	  	return sfView::SUCCESS;
  	  }
  	} 

  	return $this->redirect('@homepage');
  }
  
  public function executeDelete($request)
  {
  	$user = UserPeer::retrieveByPK($request->getParameter('id'));
  	$this->forward404Unless($user instanceof User);
  	
  	$user->delete();
  	
  	return sfView::NONE;
  }
  
  public function executeImport($request)
  {
  	if (UserPeer::retrievePublicByUsername($request->getParameter('username')) instanceof User)
  	{
  	  // send error user already exists
  	}
  	
  	// get user directory
  	$dir = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.$request->getParameter('username');
  	
  	// iterate all the files and save to db
  }
}
